/*
ID: guoxu1
PROG: prefix
LANG: C++
*/
#include <fstream>
#include <string>
#include <cstring>
#include <stack>
using namespace std;

struct Trie
{
	Trie* next[26];
	bool hasValue;
	bool hasChildren;

	Trie()
	{
		memset( next, 0, sizeof(next) );
		hasValue = false;
		hasChildren = false;
	}

	void AddStr( const char* p )
	{
		if( *p == '\0' )
		{
			hasValue = true;
			return;
		}

		hasChildren = true;
		if( next[*p - 'A'] == NULL )
		{
			next[*p - 'A'] = new Trie();
		}
		next[*p - 'A']->AddStr(p + 1);
	}

};

struct Layer
{
	int offset;
	int length;
	Trie* prim;

	Layer( int _offset, int _length, Trie* _prim ): offset(_offset), length(_length), prim(_prim)
	{
	}
};

class F
{
public:
	Trie* prims;
	string sequence;
	int length;
	int prims_max_length;

	F()
	{
		length = 0;
		prims_max_length= 0;
		prims = new Trie();
	}

	void dfs()
	{
		int seq_length = sequence.size();

		stack<Layer> stacks;
		stacks.push( Layer(0, 0, prims) );

		while( !stacks.empty() )
		{
			Layer& cur = stacks.top();
			if( cur.offset < length - prims_max_length * 2 )
			{
			  stacks.pop();
			  continue;
			}

      bool found = false;
			while( cur.length + cur.offset < seq_length && cur.prim != NULL )
			{
			  int c = sequence[cur.offset + cur.length] - 'A';
			  cur.prim = cur.prim->next[c];
			  cur.length++;
			  if( cur.prim != NULL && cur.prim->hasValue )
			  {
			    found = true;
			    break;
			  }
			}

			if( !found )
			{
			  stacks.pop();
			  continue;
			}

      int next = cur.offset + cur.length;
      if( length < next )
      {
        length = next;
        //cout << length <<endl;
      }
      stacks.push( Layer(next, 0, prims) );
		}

	}
};

#define cin fin
#define cout fout

int main()
{
	ifstream fin( "prefix.in" );
	ofstream fout( "prefix.out" );

	F f;
	string t;
	while( cin >> t, t != "." )
	{
	  if( f.prims_max_length < (int)t.size() ) f.prims_max_length = t.size();
		f.prims->AddStr( t.c_str() );
	}
	while( cin >> t )
	{
		f.sequence.append(t);
	}
	f.dfs();
	cout << f.length << endl;
	return 0;
}
